﻿var vm = new Vue({
    el: '#app',
    data: function () {
        return {
            val: '123456',
            CardInfoList: [],
            addDialogVisible: false,
            addCardInfo: {
                cardID: '',
                credenceType: '',
                elevatorAuthFloor: '',
                overTime: '',
                startTime: '',
                subDeviceID: '',
                userName: '',
                userType: '',
                uuid: ''
            },
            editDialogVisible: false,
            editCardInfo: {}
        }
    },
    created: function () {
        this.query()
    },
    methods: {
        query: function () {
            $.ajax({
                url: '/api/CardInfo/GetAllVoucher',
                type: 'post',
                dataType: 'json',
                contentType: 'application/json',
                success: function (res) {
                    console.log(res);
                    if (res.errcode !== 0) return this.$message.error(res.errmsg)
                    this.CardInfoList = res.data
                }
            })
        },
        addDialog: function () {
            this.addDialogVisible = true
        },
        saveCardInfo: function () {
            this.$refs.addCardInfoRef.validate(valid => {
                if (!valid) return false;
                $.ajax({
                    url: '/api/CardInfo/InsertVoucherInfo',
                    type: 'post',
                    dataType: 'json',
                    data: JSON.stringify(this.addCardInfo),
                    contentType: 'application/json',
                    success: (res) => {
                        if (res.errcode !== 0) return this.$message.error(res.errmsg)
                        this.$message.success('新增成功')
                        this.addDialogVisible = false
                        this.query()
                    }
                })
            })

        },
        addCancel: function () {
            this.$refs.addCardInfoRef.resetFields()
            this.addDialogVisible = false
        },
        // 编辑
        editDialog: function (row) {
            this.editCardInfo = row;
            this.editDialogVisible = true
        },
        updateCardInfo: function () {
            this.$refs.editCardInfoRef.validate(valid => {
                if (!valid) return false;
                $.ajax({
                    url: '/api/CardInfo/UpdateVoucherByID',
                    type: 'post',
                    dataType: 'json',
                    data: JSON.stringify(this.editCardInfo),
                    contentType: 'application/json',
                    success: (res) => {
                        if (res.errcode !== 0) return this.$message.error(res.errmsg)
                        this.$message.success('修改成功')
                        this.editDialogVisible = false
                        this.query()
                    }
                })
            })
        },
        editCancel: function () {
            this.$refs.editCardInfoRef.resetFields()
            this.editDialogVisible = false
        },
        // 删除
        async del: function (row) {
            var confirmRes = await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'warning'
            })
            if (confirmRes !== 'confirm') return this.$message.warning('操作取消')
            $.ajax({
                url: 'api/CardInfo/UpdateVoucherByID',
                type: 'post',
                dataType: 'json',
                data: JSON.stringify(this.editCardInfo),
                contentType: 'application/json',
                success: (res) => {
                    if (res.errcode !== 0) return this.$message.error(res.errmsg)
                    this.$message.success('修改成功')
                    this.editDialogVisible = false
                    this.query()
                }
            })
        },
        // 格式化时间
        parseTime: function (time, cFormat) {
            if (arguments.length === 0) {
                return null
            }
            const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
            let date
            if (typeof time === 'undefined' || time === null || time === 'null') {
                return ''
            } else if (typeof time === 'object') {
                date = time
            } else {
                if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
                    time = parseInt(time)
                }
                if ((typeof time === 'number') && (time.toString().length === 10)) {
                    time = time * 1000
                }
                date = new Date(time)
            }
            const formatObj = {
                y: date.getFullYear(),
                m: date.getMonth() + 1,
                d: date.getDate(),
                h: date.getHours(),
                i: date.getMinutes(),
                s: date.getSeconds(),
                a: date.getDay()
            }
            const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
                let value = formatObj[key]
                // Note: getDay() returns 0 on Sunday
                if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
                if (result.length > 0 && value < 10) {
                    value = '0' + value
                }
                return value || 0
            })
            return time_str
        }
    }
})